home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / AvantBrowser / asetup.exe / _data / webkit / chrome_100_percent.pak / Unnamed File 000037.txt < prev    next >
Text File  |  2013-04-03  |  4KB  |  111 lines

  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. // Custom bindings for the omnibox API. Only injected into the v8 contexts
  6. // for extensions which have permission for the omnibox API.
  7.  
  8. var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();
  9. var sendRequest = require('sendRequest').sendRequest;
  10.  
  11. // Remove invalid characters from |text| so that it is suitable to use
  12. // for |AutocompleteMatch::contents|.
  13. function sanitizeString(text, shouldTrim) {
  14.   // NOTE: This logic mirrors |AutocompleteMatch::SanitizeString()|.
  15.   // 0x2028 = line separator; 0x2029 = paragraph separator.
  16.   var kRemoveChars = /(\r|\n|\t|\u2028|\u2029)/gm;
  17.   if (shouldTrim)
  18.     text = text.trimLeft();
  19.   return text.replace(kRemoveChars, '');
  20. }
  21.  
  22. // Parses the xml syntax supported by omnibox suggestion results. Returns an
  23. // object with two properties: 'description', which is just the text content,
  24. // and 'descriptionStyles', which is an array of style objects in a format
  25. // understood by the C++ backend.
  26. function parseOmniboxDescription(input) {
  27.   var domParser = new DOMParser();
  28.  
  29.   // The XML parser requires a single top-level element, but we want to
  30.   // support things like 'hello, <match>world</match>!'. So we wrap the
  31.   // provided text in generated root level element.
  32.   var root = domParser.parseFromString(
  33.       '<fragment>' + input + '</fragment>', 'text/xml');
  34.  
  35.   // DOMParser has a terrible error reporting facility. Errors come out nested
  36.   // inside the returned document.
  37.   var error = root.querySelector('parsererror div');
  38.   if (error) {
  39.     throw new Error(error.textContent);
  40.   }
  41.  
  42.   // Otherwise, it's valid, so build up the result.
  43.   var result = {
  44.     description: '',
  45.     descriptionStyles: []
  46.   };
  47.  
  48.   // Recursively walk the tree.
  49.   (function(node) {
  50.     for (var i = 0, child; child = node.childNodes[i]; i++) {
  51.       // Append text nodes to our description.
  52.       if (child.nodeType == Node.TEXT_NODE) {
  53.         var shouldTrim = result.description.length == 0;
  54.         result.description += sanitizeString(child.nodeValue, shouldTrim);
  55.         continue;
  56.       }
  57.  
  58.       // Process and descend into a subset of recognized tags.
  59.       if (child.nodeType == Node.ELEMENT_NODE &&
  60.           (child.nodeName == 'dim' || child.nodeName == 'match' ||
  61.            child.nodeName == 'url')) {
  62.         var style = {
  63.           'type': child.nodeName,
  64.           'offset': result.description.length
  65.         };
  66.         result.descriptionStyles.push(style);
  67.         arguments.callee(child);
  68.         style.length = result.description.length - style.offset;
  69.         continue;
  70.       }
  71.  
  72.       // Descend into all other nodes, even if they are unrecognized, for
  73.       // forward compat.
  74.       arguments.callee(child);
  75.     }
  76.   })(root);
  77.  
  78.   return result;
  79. }
  80.  
  81. chromeHidden.registerCustomHook('omnibox', function(bindingsAPI) {
  82.   var apiFunctions = bindingsAPI.apiFunctions;
  83.  
  84.   apiFunctions.setHandleRequest('setDefaultSuggestion', function(details) {
  85.     var parseResult = parseOmniboxDescription(details.description);
  86.     sendRequest(this.name, [parseResult], this.definition.parameters);
  87.   });
  88.  
  89.   apiFunctions.setUpdateArgumentsPostValidate(
  90.       'sendSuggestions', function(requestId, userSuggestions) {
  91.     var suggestions = [];
  92.     for (var i = 0; i < userSuggestions.length; i++) {
  93.       var parseResult = parseOmniboxDescription(
  94.           userSuggestions[i].description);
  95.       parseResult.content = userSuggestions[i].content;
  96.       suggestions.push(parseResult);
  97.     }
  98.     return [requestId, suggestions];
  99.   });
  100. });
  101.  
  102. chromeHidden.Event.registerArgumentMassager('omnibox.onInputChanged',
  103.     function(args, dispatch) {
  104.   var text = args[0];
  105.   var requestId = args[1];
  106.   var suggestCallback = function(suggestions) {
  107.     chrome.omnibox.sendSuggestions(requestId, suggestions);
  108.   };
  109.   dispatch([text, suggestCallback]);
  110. });
  111.